package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueService;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {

    @Autowired
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;
    @Autowired
    private ITbClueService tbClueService;

    /**
     * 查询线索跟进记录列表
     * @param clueId
     * @return
     */
    @Override
    public List<TbClueTrackRecord> queryClueTrackRecordsByClueId(Long clueId) {
        return tbClueTrackRecordMapper.queryClueTrackRecordsByClueId(clueId);
    }

    /**
     * 新增线索跟进记录
     * @param clueTrackRecordVo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addClueTrackRecord(ClueTrackRecordVo clueTrackRecordVo) {
        // 当前登录用户名
        String username = SecurityUtils.getUsername();
        // 当前系统时间
        Date nowDate = DateUtils.getNowDate();
        TbClue tbClue = new TbClue();
        BeanUtils.copyProperties(clueTrackRecordVo, tbClue);
        tbClue.setId(clueTrackRecordVo.getClueId());
        tbClue.setStatus(TbClue.StatusType.FOLLOWING.getValue());
        tbClue.setUpdateTime(nowDate);
        tbClue.setUpdateBy(username);
        TbClueTrackRecord tbClueTrackRecord = new TbClueTrackRecord();
        BeanUtils.copyProperties(clueTrackRecordVo, tbClueTrackRecord);
        tbClueTrackRecord.setCreateBy(username);
        tbClueTrackRecord.setCreateTime(nowDate);
        // 添加线索跟进记录
        tbClueTrackRecordMapper.addClueTrackRecord(tbClueTrackRecord);
        // 更新线索
        tbClueService.updateTbClue(tbClue);
    }

    /**
     * 线索转为伪线索
     * @param tbClueTrackRecord
     */
    @Override
    public void updateRecordFalseClue(TbClueTrackRecord tbClueTrackRecord) {
        tbClueTrackRecordMapper.updateRecordFalseClue(tbClueTrackRecord);
    }
}
